package usercontroller

import (
	"context"
	"encoding/json"
	"fmt"
	"strconv"
	"strings"
	"time"
	"vpn-management-server/src/dao/redisdao"

	"vpn-management-server/logger"
	"vpn-management-server/network"
	"vpn-management-server/security/code"
	"vpn-management-server/src/dao/userdao"
	"vpn-management-server/src/model/usermodel"
	"vpn-management-server/src/service/ugroupservice"
	"vpn-management-server/src/service/userservice"
	"vpn-management-server/utils"
)

const (
	StatusPending          = 9999 // 待操作
	StatusConfirmed        = 0    // 确认
	StatusCanceled         = 1    // 取消
	StatusTimeout          = 2    // 超时
	StatusPINLock          = 3    //PIN码被锁定
	StatusSIMMessageFailed = -1   //sim消息下发失败

	ExpirationTime = 5 * 60 * time.Second // 过期时间
	RedisKeyPrefix = "SIM"
)

// SimAuth 卡认证前端接口
func SimAuth(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if user, ok := request.GetModel().(*usermodel.User); ok {
		if len(strings.Split(user.Name, "/")) != 1 {
			logger.Logger.Error("user name is illegal")
			response.Code = code.BAD_REQUEST
			response.ErrorMsg = "user name is illegal"
			return
		}

		user.Name = user.Phone
		taskId := userservice.GetTaskIdByPhone(user.Phone)

		logger.Logger.Infof("TaskId: %s", taskId)
		err := redisdao.SetRedisValue(ctx, taskId, strconv.Itoa(StatusPending), ExpirationTime)
		if err != nil {
			logger.Logger.Errorf("Failed to set Redis key for user %s", user.Name)
			response.Code = code.INTERNAL_SERVER_ERROR
			response.ErrorMsg = "Internal server error"
			return
		}

		// 存储user信息
		jsonData, err := json.Marshal(user)
		if err != nil {
			logger.Logger.Errorf("Failed to marshal data: %v", err)
			response.Code = code.INTERNAL_SERVER_ERROR
			response.ErrorMsg = "Internal server error"
			return
		}

		err = redisdao.SetRedisValue(ctx, RedisKeyPrefix+taskId, string(jsonData), ExpirationTime)
		if err != nil {
			logger.Logger.Errorf("Failed to set Redis key for user %s", user.Name)
			response.Code = code.INTERNAL_SERVER_ERROR
			response.ErrorMsg = "Internal server error"
			return
		}

		logger.Logger.Infof("PhoneNumber %s request for SIM authentication successfully", user.Name)
		response.Code = code.SUCCESS
		response.ErrorMsg = "success"
		response.Data = []byte(taskId)
	} else {
		// TODO 转换失败 回送信息
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to AdminUserJson fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to user fail"
	}
}

// SimAuthStatus 卡认证轮询接口
func SimAuthStatus(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	connID := ctx.Value("conn_id").(string)
	taskId := request.GetModel().(string)

	result, err := redisdao.GetRedisValue(ctx, taskId)
	if err != nil {
		logger.Logger.ErrorWithConn(connID, fmt.Sprintf("Error checking existence of task ID %s in Redis: %v", taskId, err))
		response.Code = code.INTERNAL_SERVER_ERROR
		response.ErrorMsg = "Internal server error"
		return
	}
	if result == "" {
		logger.Logger.ErrorWithConn(connID, fmt.Sprintf("Transaction ID %s does not exist or has expired", taskId))
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "Transaction ID does not exist or has expired"
		return
	}

	value, _ := strconv.Atoi(result)
	switch value {
	case StatusCanceled:
		logger.Logger.ErrorWithConn(connID, "Transaction cancelled by user for transaction ID: "+taskId)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "Transaction cancelled by user"
		return
	case StatusPending:
		logger.Logger.ErrorWithConn(connID, "Transaction pending confirmation for transaction ID: "+taskId)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "Transaction is pending confirmation"
		return
	case StatusTimeout:
		logger.Logger.ErrorWithConn(connID, "Transaction timed out for task ID: "+taskId)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "Transaction has timed out"
		return
	case StatusPINLock:
		logger.Logger.ErrorWithConn(connID, "PIN code locked for task ID: "+taskId)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "PIN code is locked"
		return
	case StatusSIMMessageFailed:
		logger.Logger.ErrorWithConn(connID, "SIM message delivery failed for task ID: "+taskId)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "SIM message delivery failed"
		return
	case StatusConfirmed:
		logger.Logger.Infof("Connection ID %s: Successfully retrieved data for task ID %s", connID, taskId)
	}

	// 获取user信息
	result, err = redisdao.GetRedisValue(ctx, RedisKeyPrefix+taskId)
	if err != nil {
		logger.Logger.ErrorWithConn(connID, fmt.Sprintf("Error checking existence of task ID %s in Redis: %v", taskId, err))
		response.Code = code.INTERNAL_SERVER_ERROR
		response.ErrorMsg = "Internal server error"
		return
	}

	// 反序列化 JSON 数据
	var user *usermodel.User
	err = json.Unmarshal([]byte(result), &user)
	if err != nil {
		logger.Logger.Errorf("Failed to unmarshal data: %v", err)
		response.Code = code.INTERNAL_SERVER_ERROR
		response.ErrorMsg = "Internal server error"
		return
	}

	if user2 := userdao.GetUserByName(user.Name); user2 != nil {
		logger.Logger.Infof("user %s login success\n", user2.Name)
		response.Code = code.SUCCESS
		response.Data = []byte(user2.Certificate + user2.Phone)
		return
	}

	user.Password = utils.ToMD5(user.Password)
	user.UGroupID = ugroupservice.GetUGroupIDByPhoneService(user.Phone)
	if user.UGroupID == -1 {
		logger.Logger.Error("get user group by phone fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "get user group by phone fail"
		return
	}

	if ok := userservice.AddIdentityService(user); !ok {
		logger.Logger.Errorf("user %s get identity fail\n", user.Name)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "get identity fail"
		return
	}

	err = userdao.AddUser(user)
	if err != nil {
		logger.Logger.Errorf("add user %s fail\n", user.Name)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "add user fail"
		return
	}
	logger.Logger.Infof("add user %s success\n", user.Name)
	response.Code = code.SUCCESS
	response.Data = []byte(user.Certificate + user.Phone)
	return
}

// SimAuthCallback 卡认证异步回调函数
func SimAuthCallback(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	simCallBack, ok := request.GetModel().(*usermodel.SimCallBackRequest)
	if !ok {
		logger.Logger.ErrorWithConn("Transform interface to SimCallBackRequest failed")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to SimCallBackRequest failed"
		return
	}

	if simCallBack.ResultCode != "103000" {
		logger.Logger.ErrorWithConn("Incorrect request parameters")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "Incorrect request parameters"
		return
	}

	taskId := simCallBack.Data.TaskId
	exists, _ := redisdao.RedisKeyExists(ctx, taskId)
	if !exists {
		logger.Logger.ErrorWithConn(fmt.Sprintf("taskId %s not found or expired", taskId))
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "taskId not found or expired"
		return
	}

	state := simCallBack.Data.PhoneSendStat.State
	logger.Logger.Info("User action state: " + fmt.Sprint(state))

	err := redisdao.SetRedisValue(ctx, taskId, strconv.Itoa(int(state)), ExpirationTime)
	if err != nil {
		logger.Logger.ErrorWithConn(fmt.Sprintf("Failed to update Redis for taskId %s: %v", taskId, err))
		response.Code = code.INTERNAL_SERVER_ERROR
		response.ErrorMsg = "Internal server error"
		return
	}

	logger.Logger.Infof(fmt.Sprintf("Transaction ID %s operates successfully", taskId))
	response.Code = code.SUCCESS
	response.Data = []byte("success")
}

// TokenRegister 号认证前端接口
func TokenRegister(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if user, ok := request.GetModel().(*usermodel.User); ok {
		if len(strings.Split(user.Name, "/")) != 1 {
			logger.Logger.Error("user name is illegal")
			response.Code = code.BAD_REQUEST
			response.ErrorMsg = "user name is illegal"
			return
		}
		user.Phone = userservice.TokenGetPhone(user.Token)
		if user.Phone == "" {
			logger.Logger.Error("token get phone error")
			response.Code = code.BAD_REQUEST
			response.ErrorMsg = "token get phone error"
			return
		}
		user.Name = user.Phone

		if !userdao.HasUserByName(user.Name) {
			user.Password = utils.ToMD5(user.Password)
			user.UGroupID = ugroupservice.GetUGroupIDByPhoneService(user.Name)
			if user.UGroupID == -1 {
				logger.Logger.Error("get user group by phone fail")
				response.Code = code.BAD_REQUEST
				response.ErrorMsg = "get user group by phone fail"
				return
			}

			var ok bool

			if ok = userservice.AddIdentityService(user); !ok {
				logger.Logger.Errorf("user %s get identity fail\n", user.Name)
				response.Code = code.BAD_REQUEST
				response.ErrorMsg = "get identity fail2"
				return
			}

			err := userdao.AddUser(user)
			if err != nil {
				logger.Logger.Errorf("add user %s fail\n", user.Name)
				response.Code = code.BAD_REQUEST
				response.ErrorMsg = "add user fail"
				return
			}
			logger.Logger.Infof("add user %s success\n", user.Name)
			response.Code = code.SUCCESS
			response.Data = []byte(user.Certificate + user.Phone)
			return

		} else {
			logger.Logger.Errorf("user %s is already existed\n", user.Name)
			response.Code = code.BAD_REQUEST
			response.ErrorMsg = "user exist"
			return
		}
	} else {
		// TODO 转换失败 回送信息
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to AdminUserJson fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to user fail"
	}
}

// Register MIN-VPN前端接口
func Register(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if user, ok := request.GetModel().(*usermodel.User); ok {
		if len(strings.Split(user.Name, "/")) != 1 {
			logger.Logger.Error("user name is illegal")
			response.Code = code.BAD_REQUEST
			response.ErrorMsg = "user name is illegal"
			return
		}
		if !userdao.HasUserByName(user.Name) {
			user.Password = utils.ToMD5(user.Password)
			user.UGroupID = ugroupservice.GetUGroupIDByPhoneService(user.Phone)
			if user.UGroupID == -1 {
				logger.Logger.Error("get user group by phone fail")
				response.Code = code.BAD_REQUEST
				response.ErrorMsg = "get user group by phone fail"
				return
			}

			var ok bool
			logger.Logger.Infof("here")
			if ok = userservice.AddIdentityService(user); !ok {
				logger.Logger.Errorf("user %s get identity fail\n", user.Name)
				response.Code = code.BAD_REQUEST
				response.ErrorMsg = "get identity fail"
				return
			}

			err := userdao.AddUser(user)
			if err != nil {
				logger.Logger.Errorf("add user %s fail\n", user.Name)
				response.Code = code.BAD_REQUEST
				response.ErrorMsg = "add user fail"
				return
			}
			logger.Logger.Infof("add user %s success\n", user.Name)
			response.Code = code.SUCCESS
			response.Data = []byte(user.Certificate)
			return

		} else {
			logger.Logger.Errorf("user %s is already existed\n", user.Name)
			response.Code = code.BAD_REQUEST
			response.ErrorMsg = "user exist"
			return
		}
	} else {
		// TODO 转换失败 回送信息
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to AdminUserJson fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to user fail"
	}
}

func Login(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if userLogin, ok := request.GetModel().(*usermodel.UserLogin); ok {
		if user := userdao.GetUserByName(userLogin.Name); user != nil {
			if userservice.VerifyCertificateService(user, userLogin.Signature) {
				userLogin.Password = utils.ToMD5(userLogin.Password)
				if user.Password == userLogin.Password {
					logger.Logger.Infof("user %s login success\n", userLogin.Name)
					response.Code = code.SUCCESS
					response.ErrorMsg = "login success"
					return
				} else {
					logger.Logger.Errorf("user %s password false\n", userLogin.Name)
					response.Code = code.UNAUTHORIZED
					response.ErrorMsg = "wrong password"
					return
				}
			} else {
				logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "verify user identity fail")
				response.Code = code.FORBIDDEN
				response.ErrorMsg = "verify user identity fail"
				return
			}
		} else {
			logger.Logger.Errorf("user %s not exist\n", userLogin.Name)
			response.Code = code.UNAUTHORIZED
			response.ErrorMsg = "user not exist"
			return
		}

	} else {
		// TODO 转换失败 回送信息
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to userLogin fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to userLogin fail"
		return
	}
}

func TokenLogin(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if userLogin, ok := request.GetModel().(*usermodel.UserLogin); ok {
		if user := userdao.GetUserByName(userLogin.Name); user != nil {
			logger.Logger.Infof("user %s login success\n", userLogin.Name)
			response.Code = code.SUCCESS
			response.ErrorMsg = "login success"
			return

		} else {
			logger.Logger.Errorf("user %s not exist\n", userLogin.Name)
			response.Code = code.UNAUTHORIZED
			response.ErrorMsg = "user not exist"
			return
		}
	} else {
		// TODO 转换失败 回送信息
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to userLogin fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to userLogin fail"
		return
	}
}

// SingleLogin 单点登录前端接口
func SingleLogin(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if user, ok := request.GetModel().(*usermodel.User); ok {
		if len(strings.Split(user.Name, "/")) != 1 {
			logger.Logger.Error("user name is illegal")
			response.Code = code.BAD_REQUEST
			response.ErrorMsg = "user name is illegal"
			return
		}

		user.Phone = userservice.GetPhoneForSingleLogin(user.Token)
		if user.Phone == "" {
			logger.Logger.Error("token get phone error")
			response.Code = code.BAD_REQUEST
			response.ErrorMsg = "token get phone error"
			return
		}

		user.Name = user.Phone
		if user2 := userdao.GetUserByName(user.Name); user2 != nil {
			logger.Logger.Infof("user %s login success\n", user2.Name)
			response.Code = code.SUCCESS
			response.Data = []byte(user2.Certificate + user2.Phone)
			return
		}

		user.Password = utils.ToMD5(user.Password)
		user.UGroupID = ugroupservice.GetUGroupIDByPhoneService(user.Phone)
		if user.UGroupID == -1 {
			logger.Logger.Error("get user group by phone fail")
			response.Code = code.BAD_REQUEST
			response.ErrorMsg = "get user group by phone fail"
			return
		}

		if ok := userservice.AddIdentityService(user); !ok {
			logger.Logger.Errorf("user %s get identity fail\n", user.Name)
			response.Code = code.BAD_REQUEST
			response.ErrorMsg = "get identity fail"
			return
		}

		err := userdao.AddUser(user)
		if err != nil {
			logger.Logger.Errorf("add user %s fail\n", user.Name)
			response.Code = code.BAD_REQUEST
			response.ErrorMsg = "add user fail"
			return
		}
		logger.Logger.Infof("add user %s success\n", user.Name)
		response.Code = code.SUCCESS
		response.Data = []byte(user.Certificate + user.Phone)
		return

	} else {
		// TODO 转换失败 回送信息
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to AdminUserJson fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to user fail"
	}
}

func Reset(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if userReset, ok := request.GetModel().(*usermodel.UserReset); ok {
		userReset.OldPassword = utils.ToMD5(userReset.OldPassword)
		if user := userdao.GetUserByName(userReset.Name); user != nil {
			if userservice.VerifyCertificateService(user, userReset.Signature) {
				if user.Password == userReset.OldPassword {
					if err := userdao.UpdateUserPasswordAndTimeStamp(userReset.Name, utils.ToMD5(userReset.NewPassword), userReset.TimeStamp); err != nil {
						logger.Logger.Errorf("user %s reset password fail\n", userReset.Name)
						response.Code = code.INTERNAL_SERVER_ERROR
						response.ErrorMsg = "reset password fail"
						return
					}
					logger.Logger.Infof("user %s reset success\n", userReset.Name)
					response.Code = code.SUCCESS
					response.ErrorMsg = "reset success"
					return
				} else {
					logger.Logger.Errorf("user %s password false\n", userReset.Name)
					response.Code = code.UNAUTHORIZED
					response.ErrorMsg = "wrong password"
					return
				}
			} else {
				logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "verify user identity fail")
				response.Code = code.FORBIDDEN
				response.ErrorMsg = "verify user identity fail"
				return
			}
		} else {
			logger.Logger.Errorf("user %s not exist\n", userReset.Name)
			response.Code = code.UNAUTHORIZED
			response.ErrorMsg = "user not exist"
			return
		}
	} else {
		// TODO 转换失败 回送信息
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to userReset fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to userReset fail"
		return
	}
}

// DeleteUserByID 管理员前端接口
func DeleteUserByID(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if userNameInfo, ok := request.GetModel().(*usermodel.UserNameInfo); ok {
		// TODO 判断是否存在用户
		if userdao.HasUserByName(userNameInfo.Name) {
			err := userservice.DeleteUserIdentityFromBCSSL("/" + userNameInfo.Name)
			if err != nil {
				logger.Logger.ErrorfWithConn(ctx.Value("conn_id").(string), err.Error())
				response.Code = code.INTERNAL_SERVER_ERROR
				response.ErrorMsg = err.Error()
				return
			}

			err = userservice.DeleteUserService(userNameInfo.ID)
			if err != nil {
				logger.Logger.ErrorfWithConn(ctx.Value("conn_id").(string), err.Error())
				response.Code = code.INTERNAL_SERVER_ERROR
				response.ErrorMsg = err.Error()
				return
			}
			response.Code = code.SUCCESS
			logger.Logger.Infof("delete user %s success\n", userNameInfo.Name)
			return
		} else {
			logger.Logger.ErrorfWithConn(ctx.Value("conn_id").(string), "user %s not exist\n", userNameInfo.Name)
			response.Code = code.NOT_FOUND
			response.ErrorMsg = "user not exist"
			return
		}
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to userNameInfo fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to userNameInfo fail"
		return
	}
}

// DeleteUserByName 区块链接口
func DeleteUserByName(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if userName, ok := request.GetModel().(*usermodel.UserName); ok {

		if user := userdao.GetUserByName(userName.Name); user != nil {
			err := userservice.DeleteUserService(user.ID)
			if err != nil {
				logger.Logger.ErrorfWithConn(ctx.Value("conn_id").(string), err.Error())
				response.Code = code.INTERNAL_SERVER_ERROR
				response.ErrorMsg = err.Error()
				return
			}
			response.Code = code.SUCCESS
			logger.Logger.Infof("delete user %s success\n", userName.Name)
			return
		} else {
			logger.Logger.ErrorfWithConn(ctx.Value("conn_id").(string), "user %s not exist\n", userName.Name)
			response.Code = code.NOT_FOUND
			response.ErrorMsg = "user not exist"
			return
		}
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to userName fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to userName fail"
		return
	}
}

func RevokeUserByID(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if userNameInfo, ok := request.GetModel().(*usermodel.UserNameInfo); ok {
		// TODO 判断是否存在用户
		if userdao.HasUserByName(userNameInfo.Name) {
			err := userdao.RevokeUserByID(userNameInfo.ID)
			if err != nil {
				logger.Logger.ErrorfWithConn(ctx.Value("conn_id").(string), "revoke user %s fail\n", userNameInfo.Name)
				response.Code = code.INTERNAL_SERVER_ERROR
				response.ErrorMsg = "revoke user fail"
				return
			}
			response.Code = code.SUCCESS
			return
		} else {
			logger.Logger.ErrorfWithConn(ctx.Value("conn_id").(string), "user %s not exist\n", userNameInfo.Name)
			response.Code = code.NOT_FOUND
			response.ErrorMsg = "user not exist"
			return
		}
	} else {
		// TODO 转换失败 回送信息
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to userNameInfo fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to userNameInfo fail"
		return
	}
}

func RecoverUserByID(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if userNameInfo, ok := request.GetModel().(*usermodel.UserNameInfo); ok {
		// TODO 判断是否存在用户
		if userdao.HasUserByName(userNameInfo.Name) {
			err := userdao.RecoverUserByID(userNameInfo.ID)
			if err != nil {
				logger.Logger.ErrorfWithConn(ctx.Value("conn_id").(string), "recover user %s fail\n", userNameInfo.Name)
				response.Code = code.INTERNAL_SERVER_ERROR
				response.ErrorMsg = "recover user fail"
				return
			}
			response.Code = code.SUCCESS
			return
		} else {
			logger.Logger.ErrorfWithConn(ctx.Value("conn_id").(string), "user %s not exist\n", userNameInfo.Name)
			response.Code = code.NOT_FOUND
			response.ErrorMsg = "user not exist"
			return
		}
	} else {
		// TODO 转换失败 回送信息
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to userNameInfo fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to userNameInfo fail"
		return
	}
}

// GetUserByName 搜索框功能 通过名字找到对应的用户信息
func GetUserByName(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if userNameInfo, ok := request.GetModel().(*usermodel.UserNameInfo); ok {
		user := userdao.GetUserByName(userNameInfo.Name)
		if user == nil {
			logger.Logger.ErrorfWithConn(ctx.Value("conn_id").(string), "get user %s info fail\n", userNameInfo.Name)
			response.Code = code.INTERNAL_SERVER_ERROR
			response.ErrorMsg = "get user info fail"
			return
		}
		data, _ := json.Marshal(user)
		response.Code = code.SUCCESS
		response.Data = data
		return
	} else {
		// TODO 转换失败 回送信息
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to userNameInfo fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to userNameInfo fail"
		return
	}
}

func ResetUserPassword(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if userReset, ok := request.GetModel().(*usermodel.UserResetByAdmin); ok {
		userReset.NewPassword = utils.ToMD5(userReset.NewPassword)
		if user := userdao.GetUserByName(userReset.Name); user != nil {
			if err := userdao.UpdateUserPasswordAndTimeStamp(userReset.Name, utils.ToMD5(userReset.NewPassword), userReset.TimeStamp); err != nil {
				logger.Logger.Errorf("user %s reset password fail\n", userReset.Name)
				response.Code = code.INTERNAL_SERVER_ERROR
				response.ErrorMsg = "reset password fail"
				return
			}
			logger.Logger.Infof("user %s reset success\n", userReset.Name)
			response.Code = code.SUCCESS
			response.ErrorMsg = "reset success"
			return
		} else {
			logger.Logger.Errorf("user %s not exist\n", userReset.Name)
			response.Code = code.UNAUTHORIZED
			response.ErrorMsg = "user not exist"
			return
		}
	} else {
		// TODO 转换失败 回送信息
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to userReset fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to userReset fail"
		return
	}
}
